kerasProblem:
The task is to train a network to discriminate between sonar signals bounced off a metal cylinder and those bounced off a roughly cylindrical rock.
Source: https://archive.ics.uci.edu/ml/datasets/Connectionist+Bench+(Sonar,+Mines+vs.+Rocks)
Data Set Information:
The file "sonar.mines" contains 111 patterns obtained by bouncing sonar signals off a metal cylinder at various angles and under various conditions. The file "sonar.rocks" contains 97 patterns obtained from rocks under similar conditions. The transmitted sonar signal is a frequency-modulated chirp, rising in frequency. The data set contains signals obtained from a variety of different aspect angles, spanning 90 degrees for the cylinder and 180 degrees for the rock.
Each pattern is a set of 60 numbers in the range 0.0 to 1.0. Each number represents the energy within a particular frequency band, integrated over a certain period of time. The integration aperture for higher frequencies occur later in time, since these frequencies are transmitted later during the chirp.
The label associated with each record contains the letter "R" if the object is a rock and "M" if it is a mine (metal cylinder). The numbers in the labels are in increasing order of aspect angle, but they do not encode the angle directly.
In [ ]:
import numpy as np
import pandas as pd
In [ ]:
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
In [ ]:
#Read the dataset
data = pd.read_csv("../data/sonar.csv", header=None)
In [ ]:
#View the first 5 records
In [ ]:
#Find number of rows and columns in data
In [ ]:
#Find count of R and M in the target
In [ ]:
# split into input (X) and output (Y) variables
data = data.values
X = data[:,0:60].astype(float)
y = data[:,60]
In [ ]:
#Import keras and scikit-learn libraries.
from keras.models import Sequential
from keras.layers import Dense
from sklearn.cross_validation import train_test_split
In [ ]:
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(y)
encoded_y = encoder.transform(y)
In [ ]:
X_train, X_test, y_train, y_test = train_test_split( X, encoded_y,
test_size=0.2,
random_state=seed)
In [ ]:
# Sanity check: If the target variable is
#distributed similarly in both train and test.
#If you think it isn't, you could use stratified sampling.
#For this notebook, we will stick to what we have gotten.
In [ ]:
np.unique(y_train, return_counts=True)
In [ ]:
np.unique(y_test, return_counts=True)
In [ ]:
model = Sequential()
In [ ]:
model.add(Dense(60, input_dim=60, init='normal', activation='relu'))
In [ ]:
model.add(Dense(1, init='normal', activation='sigmoid'))
In [ ]:
# Compile model
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
In [ ]:
nb_epoch = 100
batch_size = 5
verbose = 1
In [ ]:
model.fit(X_train, y_train, nb_epoch=nb_epoch,
batch_size=batch_size, verbose=verbose)
In [ ]:
score = model.evaluate(X_test, y_test, batch_size=batch_size)
In [ ]:
score
In [ ]:
#In the below code
# - add another dense layer - with 30 neurons.
# - Set the activation to sigmoid
# - Observe what happens if you set the activation to relu
In [ ]:
#Modify this code ! Add one more layer with activation sigmoid
model = Sequential()
model.add(Dense(60, input_dim=60, init='normal', activation='sigmoid'))
model.add(Dense(1, init='normal', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, nb_epoch=nb_epoch,
batch_size=batch_size, verbose=verbose)
score = model.evaluate(X_test, y_test, batch_size=batch_size)
In [ ]:
score
In [ ]:
#Modify this code ! Add one more layer with activation relu
model = Sequential()
model.add(Dense(60, input_dim=60, init='normal', activation='relu'))
model.add(Dense(1, init='normal', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, nb_epoch=nb_epoch,
batch_size=batch_size, verbose=verbose)
score = model.evaluate(X_test, y_test, batch_size=batch_size)
In [ ]:
score
In [ ]: